<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午9:54
 */

class PlatformService extends Service {

    /**
     * 根据平台ID获取一条平台信息
     */
    public static function getPlatformById($id){
        $modPlatform = new ModPlatform();

        $platform = $modPlatform->findByPk($id)->attributes;

        return $platform;
    }

    /**
     * 平台首页表头
     */
    public static function indexHeader(){
        return array(
            'PlatformID'    => array('text' => PubFunc::t('平台ID')),
            'PlatformSrc'   => array('text' => PubFunc::t('平台代号')),
            'PlatformName'  => array('text' => PubFunc::t('平台名称')),
            'Action'        => array('text' => PubFunc::t('操作')),
        );
    }

    /**
     * 平台首页数据
     * @param $params
     */
    public static function indexData($params){
        $modPlatform = new ModPlatform();
        $result = $modPlatform->select($params);

        $editText = PubFunc::t('编辑');
        $grantText = PubFunc::t('游戏');

        foreach($result as &$v){
            $v['Action'] = "<a href='/auth/?r=platform/edit&id={$v['PlatformID']}'>{$editText}</a>";
        }
        unset($v);

        return $result;
    }

    /**
     * 新增平台
     * @param $params
     * @return bool
     */
    public static function add($params){
        $modPlatform = new ModPlatform();

        $modPlatform->PlatformID     = $params['id'];
        $modPlatform->PlatformName   = $params['name'];
        $modPlatform->PlatformSrc    = $params['src'];

        PubFunc::addBy($modPlatform);

        if($modPlatform->save()){
            $platformId = $modPlatform->getDbConnection()->getLastInsertID();

            $me = AccAuth::me();
            $addBy = $me['UserID'];
            $addTime = date('Y-m-d H:i:s');

            if(!empty($params['game']) && is_array($params['game'])){
                foreach($params['game'] as $v){
                    $map[] = array(
                        'PlatformID' => $platformId,
                        'GameID'     => $v,
                        'AddBy'      => $addBy,
                        'AddTime'    => $addTime
                    );
                }
            }

            $modPlatformGameMap = new ModPlatformGameMap();
            $modPlatformGameMap->addMapByPlatform($platformId, $map);

            return true;
        }else{
            return false;
        }
    }

    /**
     * 编辑平台
     * @param $params
     * @return bool
     */
    public static function edit($params){
        $id = $params['id'];

        $map = array();
        $me = AccAuth::me();
        $addBy = $me['UserID'];
        $addTime = date('Y-m-d H:i:s');

        $attr = array(
            'PlatformName'   => $params['name'],
            'PlatformSrc'    => $params['src'],
            'PlatformType'   => $params['type'],
            'EditBy'         => $addBy,
            'EditTime'       => $addTime
        );

        if(!empty($params['game']) && is_array($params['game'])){
            foreach($params['game'] as $v){
                $map[] = array(
                    'PlatformID' => $id,
                    'GameID'     => $v,
                    'AddBy'      => $addBy,
                    'AddTime'    => $addTime
                );
            }
        }

        try{
            self::beginTransaction();

            $modPlatform = new ModPlatform();
            $modPlatform->updateByPk($id, $attr);

            $modPlatformGameMap = new ModPlatformGameMap();
            $modPlatformGameMap->addMapByPlatform($id, $map);

            self::commit();
            return true;
        }catch (Exception $e){
            self::rollback();
            return false;
        }
    }

    public static function getOptions(){
        $modPlatform = new ModPlatform();
        $tmp = $modPlatform->select();

        $result = array();
        if(!empty($tmp) && is_array($tmp)){
            foreach($tmp as $v){
                $result[$v['PlatformID']] = $v['PlatformName'];
            }
        }

        return $result;
    }
} 